﻿using System;
using System.Configuration;
using System.Text.RegularExpressions;

namespace Company.Merchandising.ServiceLayer.EntityManagement.Credentials
{
    public class CredentialsManagerSection : ConfigurationSection
    {
        private const string LoginAttemptsKey = "loginAttempts";
        [ConfigurationProperty(LoginAttemptsKey, DefaultValue = 5)]
        public int LoginAttempts
        {
            get
            {
                return (int)this[LoginAttemptsKey];
            }

            set
            {
                this[LoginAttemptsKey] = value;
            }
        }

        private const string LockAccountTimeKey = "accountLockTime";
        [ConfigurationProperty(LockAccountTimeKey, DefaultValue = 30)]
        public int LockAccountTime
        {
            get
            {
                return (int)this[LockAccountTimeKey];
            }

            set
            {
                this[LockAccountTimeKey] = value;
            }
        }

        private const string PasswordRegexKey = "passwordRegex";
        [ConfigurationProperty("passwordRegex", DefaultValue = "[a-z]+")]
        public string PasswordRegex
        {
            get
            {
                return (string)this[PasswordRegexKey];
            }

            set
            {
                this[PasswordRegexKey] = value;
            }
        }

        private const string LoginRegexKey = "loginRegex";
        [ConfigurationProperty(LoginRegexKey, DefaultValue = "[a-z]+")]
        public string LoginRegex
        {
            get
            {
                return (string)this[LoginRegexKey];
            }

            set
            {
                this[LoginRegexKey] = value;
            }
        }

		private const string PasswordChangePeriodKey = "passwordChangePeriod";
		[ConfigurationProperty(PasswordChangePeriodKey, DefaultValue = 30)]
		public int PasswordChangePeriod
		{
			get
			{
				return (int)this[PasswordChangePeriodKey];
			}

			set
			{
				this[PasswordChangePeriodKey] = value;
			}
		}

        public void ValidateSection()
        {
            try
            {
                new Regex(PasswordRegex);
            }
            catch (ArgumentException ex)
            {
                throw new ConfigurationErrorsException("Invalid passwordRegex parameter value", ex);
            }

            try
            {
                new Regex(LoginRegex);
            }
            catch (Exception ex)
            {
                throw new ConfigurationErrorsException("Invalid loginRegex parameter value", ex);
            }

			if (LoginAttempts <= 0)
			{
				throw new ConfigurationErrorsException("loginAttempts parameter must be positive number");
			}

			if (LockAccountTime < 0)
			{
				throw new ConfigurationErrorsException("accountLockTime parameter must be positive number or equal 0");
			}

			if (PasswordChangePeriod <= 0)
			{
				throw new ConfigurationErrorsException("passwordChangePeriod parameter must be positive number");
			}
        }
    }
}
